package com.whq.thrift.api.web.base.serviceconf.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whq.thrift.api.common.spring.datasource.DataSourceContextHolder;
import com.whq.thrift.api.web.base.serviceconf.db.ModuleInstance;
import com.whq.thrift.api.web.base.serviceconf.db.ModuleInstanceMapper;
import com.whq.thrift.api.web.base.serviceconf.service.ModuleInstanceService;

@Service
public class ModuleInstanceServiceImpl implements ModuleInstanceService{
	
	@Autowired
	private ModuleInstanceMapper moduleInstanceMapper;
	
	public void addRecord(ModuleInstance record) {
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		if (record == null) {
			return ;
		}
		Date now  = new Date();
		record.setCreateTime(now);
		record.setUpdateTime(now);
		
		this.moduleInstanceMapper.addRecord(record);
	}
	
	public List<ModuleInstance> findRecordsByMap(String moduleId, int beginIndex, int maxCount) {
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		
//		if (whereParams == null) {
//			whereParams = new HashMap<String, Object>(0);
//		}
//		whereParams.put("beginIndex", beginIndex);
//		whereParams.put("maxCount", maxCount);
		return this.moduleInstanceMapper.findRecordsByMap(moduleId, beginIndex, maxCount);
	}
	
	public void updateRecord(ModuleInstance record) {
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		if (record == null) {
			return ;
		}
		Date now  = new Date();
		record.setUpdateTime(now);
		
		this.moduleInstanceMapper.updRecord(record);
	}
	
	public void deleteRecord(String id) {
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		
		this.moduleInstanceMapper.delRecord(id);
	}

	public int findRecordCount(String moduleId) {
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		
		return this.moduleInstanceMapper.findRecordsCount(moduleId);
	}
	
//	public ModuleInstance findRecordById(String id) {
//		DataSourceContextHolder.set("dataSourceBaseServiceConfig");
//		
//		return this.moduleInstanceMapper.getRecordByKey(id);
//	}
	
	public List<ModuleInstance> getAllRecords(){
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		
		return this.moduleInstanceMapper.getAllRecords();
	}
	
	public List<ModuleInstance> getRecordsByStatus(String moduleId, int status){
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		
		return this.moduleInstanceMapper.getRecordByStatus(moduleId, status);
	}

}
